/**
 * jqGrid 改造与视觉优化调整
 * @author 329202 <a href="mailto:fuyuxin@sf-express.com">符瑜鑫(Ricky Fu)</a>
 * @createDate 2012-07-21
 */
(function($){
	var jqg = $.fn.jqGrid;
	
	$.jgrid.extend({
		perfect: function(){
			var $grid = this, el = $grid.get(0), config = el.p,
				rsFn = config['resizeStop'], gcFn = config['gridComplete'],
				$ctx = $grid.parents('div.ui-jqgrid').removeAttr('style'),
				$gview = $ctx.find('div.ui-jqgrid-view'),
				$hb = $ctx.find('div.ui-jqgrid-hdiv, div.ui-jqgrid-bdiv'),
				setWid = function(){
					var gw = $grid.width(), ctxw = $ctx.width();
					$hb.width(gw < ctxw ? ctxw : gw+18);
				};
			$gview.css({'width':'100%','overflow-x':'auto','overflow-y':'hidden','position':'relative','zoom':1});
			$ctx.find('.ui-jqgrid-pager').removeAttr('style');
			setWid();
			config['resizeStop'] = function(nw, idx){
				setWid();
				rsFn ? rsFn.call($grid, nw, idx) : '';
			};
			config['gridComplete'] = function(){
				setWid();
				gcFn ? gcFn.call($grid) : '';
			};
		}
	});
	
	$.fn.jqGrid = function(pin){
		if (typeof pin == 'string') {//此部分为jqGrid源码
			var fn = $.jgrid.getAccessor(jqg, pin);
			if (!fn) {
				throw ("jqGrid - No such method: " + pin);
			}
			var args = $.makeArray(arguments).slice(1);
			return fn.apply(this,args);
		}
		var $tmp = $('<div style="width:100%;"></div>').insertAfter(this), tw = $tmp.width()-18, gw = 0;
		$.each(pin['colModel'], function(){
			if(this.width === undefined) {
				this.width = 100;
			}
			gw += this.width;
		});
		if(gw > tw) {
			pin.autowidth = false;//取消此配置
		}
		$tmp.remove();
		jqg.call(this, pin).perfect();
	}
	
})(jQuery);